package com.sangu.dianxiu.ui;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;

import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSONObject;
import com.android.volley.VolleyError;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.sangu.dianxiu.BaseApp;
import com.sangu.dianxiu.ICallBack;
import com.sangu.dianxiu.R;
import com.sangu.dianxiu.bean.user.LoginEntity;
import com.sangu.dianxiu.bean.user.User;
import com.sangu.dianxiu.bean.user.VerificationCodeEntity;
import com.sangu.dianxiu.config.Constants;
import com.sangu.dianxiu.utils.CheckUtils;
import com.sangu.dianxiu.utils.UserManager;

import java.util.HashMap;

public class LoginActivity extends BaseActivity {

    UserManager userManager=new UserManager();
    @ViewInject(R.id.activity_login_back_iv)    //使用xUtils进行注解，这样就不用findViewById了
    private ImageView ivBack;
    @ViewInject(R.id.et_activity_login_phone)
    private EditText etPhone;
    @ViewInject(R.id.et_activity_login_veritycode)
    private EditText etVerityCode;
    @ViewInject(R.id.tv_activity_login_getveritycode)
    private TextView tvGetVerityCode;
    @ViewInject(R.id.btn_activity_login_login)
    private Button btnLogin;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ViewUtils.inject(this);     //调用此方法完成注解
    }
    //使用XUtils的点击事件注解，这样就不用再setOnClickListener了
    @OnClick({R.id.activity_login_back_iv,R.id.tv_activity_login_getveritycode
    ,R.id.btn_activity_login_login})
    public void onClick(View view)
    {
        switch (view.getId())
        {
            case R.id.activity_login_back_iv:
                finish();
                break;
            case R.id.tv_activity_login_getveritycode:
                if(!TextUtils.isEmpty(etPhone.getText()))
                {
                    getVerificationCode();
                }
                else
                {
                    Toast.makeText(this,"请输入手机号",Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.btn_activity_login_login:
                if(!TextUtils.isEmpty(etPhone.getText())&&!TextUtils.isEmpty(etVerityCode.getText()))
                {
                    login();
                }
                else
                {
                    Toast.makeText(this,"请输入手机号和验证码",Toast.LENGTH_SHORT).show();
                }
                break;
        }

    }

    public void getVerificationCode() {
        HashMap<String,String> map=new HashMap<>();
        map.put("phone",etPhone.getText().toString().trim());
        userManager.getVerificationCode(map, new ICallBack() {
            @Override
            public void onSuccess(String string) {
                VerificationCodeEntity entity=JSONObject.parseObject(string,VerificationCodeEntity.class);
                if(entity.getRc()==0)
                {
                    Toast.makeText(LoginActivity.this,entity.getVcode(),Toast.LENGTH_LONG).show();
                }
            }
            @Override
            public void onError(VolleyError error) {

            }
        });
    }
    private void login() {
        HashMap<String,String> map=new HashMap<>();
        map.put("phone",etPhone.getText().toString().trim());
        map.put("vcode", etVerityCode.getText().toString().trim());
        userManager.login(map, new ICallBack() {
            @Override
            public void onSuccess(String string) {
                LoginEntity entity= JSONObject.parseObject(string,LoginEntity.class);
                if(entity.getRc()==0)
                {
                    BaseApp.isLogin=true;
                    SharedPreferences preferences=getSharedPreferences(Constants.IS_FIRST_RUN, Context.MODE_PRIVATE);
                    preferences.edit().putInt(Constants.USER_ID,entity.getUserId()).commit();                   //登录成功后要将服务器返回的相应数据存起来，这样就不用每次启动app就要重新登录
                    preferences.edit().putString(Constants.PASSWORD,entity.getPwd()).commit();
                    preferences.edit().putString(Constants.PHONE_NUMBER,etPhone.getText().toString().trim()).commit();
                    BaseApp.user=new User();
                    BaseApp.user.setUserId(entity.getUserId());
                    BaseApp.user.setUserPhone(etPhone.getText().toString().trim());
                    Intent intent=new Intent();
                    setResult(Constants.REQUEST_CODE_LOGIN,intent);
                    LoginActivity.this.finish();
                }
            }

            @Override
            public void onError(VolleyError error) {

            }
        });

    }
    //当销毁Activity的时候取消网络请求
    @Override
    protected void onDestroy() {
        super.onDestroy();
        userManager.requestQueue.cancelAll(UserManager.class);
    }
}
